package com.lxxie.http;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Arrays;

/**
 * @Description TODO
 * @Date 2021/10/27 11:32
 * @Created by xielx
 */
public class DowladJpg {
    public static void main(String[] args) throws IOException {
        String url = "http://sc.122.gov.cn:9000/group1/M00/D6/2B/wKjITWF7T16AaxiaAAZf5WFK16s133.jpg";
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet request = new HttpGet(url);
        CloseableHttpResponse resp = httpClient.execute(request);
        byte[] bs = EntityUtils.toByteArray(resp.getEntity());
//        FileOutputStream fos = new FileOutputStream("d://12.jpg");
//        fos.write(bs);
//        fos.close();
        String str = Base64.encodeBase64String(bs);
        System.out.println(233);
        //getSuffix(bs);

    }

    private static void getSuffix(byte[] bs) {
        byte[] tbs = Arrays.copyOf(bs, 4);
        String type = Hex.encodeHexString(tbs).toUpperCase();
        System.out.println(type);
        if (type.contains("FFD8FF")) {
            System.out.println("jpg");
        } else if (type.contains("89504E47")) {
            System.out.println("png");
        } else if (type.contains("47494638")) {
            System.out.println("gif");
        } else if (type.contains("424D")) {
            System.out.println("bmp");
        }else if(type.contains("49492A00")){
            System.out.println("tif");
        }
    }
}
